package com.boge.study.test.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class CopyFile {
	
	public static void main(String[] args) {
		copyFile();
	}
	 
	public static void copyFile() {
		String courseFile = System.getProperty("user.dir");
		String filePath1 = courseFile + "/testFile/copyFrom";
		String filePath2 = courseFile + "/testFile/copyTo";
		String filePath3 = courseFile + "/testFile/login_back1.jpg";
		String filePath4 = courseFile + "/testFile/login_back2.jpg";
		
		Reader reader = null;
		Writer writer = null;
		
		InputStream inputStream = null;
		OutputStream outputStream = null;
		
		try {
			//方法一：按字符读取
//			reader = new FileReader(filePath1);
//			writer = new FileWriter(filePath2);
//			
//			int tempChar;
//			while ((tempChar = reader.read()) != -1) {
//				System.out.println((char)tempChar);
//				writer.append((char)tempChar);
////				writer.flush();
//			}
			
			//方法二：按字节读取 用于复制字节文件（图片，影音等）
			inputStream = new FileInputStream(filePath3);
			outputStream = new FileOutputStream(filePath4);
			
			byte[] bytes = new byte[1024];
			int len;
			while ((len = inputStream.read(bytes)) != -1) {
				System.out.println(len);
				outputStream.write(bytes);
			}
			System.out.println("文件复制成功");
			
		} catch (IOException e) {
			// TODO: handle exception
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
				if (writer != null) {
					writer.close();
				}
				if (inputStream != null) {
					inputStream.close();
				}
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		
	}

}
